import 'package:flutter/material.dart';
import 'package:flutter_module/beans/project_bean.dart';
import 'package:flutter_module/viewModels/layer_edit_viewmodel.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

///项目编辑页面
class ProjectEditFragment extends StatefulWidget {
  const ProjectEditFragment({Key? key}) : super(key: key);

  @override
  State<ProjectEditFragment> createState() => _ProjectEditFragmentState();
}

class _ProjectEditFragmentState extends State<ProjectEditFragment> {
  ///名称输入框控制器
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _describeController = TextEditingController();
  late final LayerEditViewModel _model =
      Provider.of<LayerEditViewModel>(context, listen: false);

  @override
  void initState() {
    _nameController.addListener(() {
      if (_model.curEditProject != null) {
        _model.curEditProject!.name = _nameController.text;
      }
    });
    _describeController.addListener(() {
      if (_model.curEditProject != null) {
        _model.curEditProject!.describe = _describeController.text;
      }
    });
    super.initState();
  }

  @override
  void didChangeDependencies() {
    if (_model.curEditProject != null) {
      _nameController.text = _model.curEditProject!.name;
      _describeController.text = _model.curEditProject!.describe;
    }
    super.didChangeDependencies();
  }

  @override
  void dispose() {
    _describeController.dispose();
    _nameController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final NavigatorState _navi = Navigator.of(context);
    return Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            ///不创建就销毁
            _model.disposeEditProject();
            _navi.pop("back");
          },
        ),
        actions: [
          ///保存按钮
          TextButton(
            onPressed: () {
              ///名称必须输入
              if (_nameController.text.isEmpty) {
                Fluttertoast.showToast(msg: "请输入项目名称");
              } else {
                _model.saveEditProject().then((value) {
                  if (value) {
                    _navi.pop("success");
                  } else {
                    Fluttertoast.showToast(msg: "数据保存失败");
                  }
                });
              }
            },
            child: const Text(
              "保存",
              style: TextStyle(color: Colors.white),
            ),
          )
        ],
      ),
      body: Column(
        children: [
          TextField(
            controller: _nameController,
            decoration: InputDecoration(
              labelText: '项目名称: ',
              hintText: "请输项目名称",
              labelStyle: const TextStyle(color: Colors.red),
              icon: const Icon(Icons.insert_drive_file),
              suffixIcon: IconButton(
                tooltip: '清空',
                icon: const Icon(Icons.clear),
                onPressed: () {
                  _nameController.text = "";
                },
              ),
            ),
          ),
          const Text("备注"),
          TextFormField(
            controller: _describeController,
            autofocus: false,
            autocorrect: false,
            keyboardType: TextInputType.multiline,
            minLines: 8,
            maxLines: 8,
            decoration: const InputDecoration(
              filled: true,
              hintText: "可输入描述信息",
              fillColor: Color(0xFFF2F2F2),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(4)),
                borderSide: BorderSide(width: 1),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
